package com.lyc.controller;

import com.github.pagehelper.PageInfo;
import com.lyc.pojo.Goods;
import com.lyc.pojo.GoodsInfo;
import com.lyc.pojo.GoodsKinds;
import com.lyc.service.GoodsService;
import com.lyc.tool.AjaxResponse;
import com.lyc.tool.Msg1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/goods")//请求路径
@CrossOrigin
public class GoodsController {
    /*属性区*/
    @Autowired
    private GoodsService goodsService;//私有属性

    /*方法区*/
    //查询所有商品
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @ResponseBody
    public List<Goods> selectAll(){
        List<Goods> list = goodsService.selectAll();
        return list;
    }
    //查询指定条件商品
    @RequestMapping(value = "/all/{gmiaoshu}",method = RequestMethod.GET)
    @ResponseBody
    public List<Goods> selectByGmiaoshu(@PathVariable("gmiaoshu")String gmiaoshu){
        try {
            gmiaoshu= new String(gmiaoshu.getBytes("iso-8859-1"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        List<Goods> list = goodsService.selectByGmiaoshu(gmiaoshu);

        return list;
    }




    //根据商品模糊查询
    @RequestMapping(value = "/selectGoodsByLike/{goodsKeyword}",method = RequestMethod.GET)
    @ResponseBody
    public List<Goods> selectsp(@PathVariable("goodsKeyword") String goodsKeyword){
        try {
             String s = new String(goodsKeyword.getBytes("iso-8859-1"),"utf-8");
            List<Goods> selectsp = goodsService.selectsp(s);
            return selectsp;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    //查询一级分类
    @ResponseBody
    @RequestMapping(value = "/selectGoodsByOne",method = RequestMethod.GET)
    public List<GoodsKinds> selectid(){
        List<GoodsKinds> goodsKinds = goodsService.selectid();
        return goodsKinds;
    }

    //通过一级查二级
    @RequestMapping(value = "/selectGoodsByOneSid",method =RequestMethod.GET)
    @ResponseBody
    public List<GoodsKinds> selectid1(Integer gkid){

        List<GoodsKinds> goodsKinds = goodsService.selectid1(gkid);
        return goodsKinds;
    }
    //通过二级查三级
    @RequestMapping(value = "/selectGoodsByTwoSid/{gkid}",method =RequestMethod.GET)
    @ResponseBody
    public List<GoodsKinds> selectid2(@PathVariable("gkid") Integer gkid){

        List<GoodsKinds> goodsKinds2 = goodsService.selectid2(gkid);
        return goodsKinds2;
    }
   //通过三级查四级
    @RequestMapping(value = "/selectGoodsByThreeSid/{gkid}",method =RequestMethod.GET)
    @ResponseBody
    public List<GoodsKinds> selectid3(@PathVariable("gkid") Integer gkid){

        List<GoodsKinds> goodsKinds3 = goodsService.selectid3(gkid);
        return goodsKinds3;
    }
    //通过一级查四级
    @RequestMapping(value = "/selectGoodsOneByFour/{gkid}",method = RequestMethod.GET)
    @ResponseBody
    public List<Goods> selectid4(@PathVariable("gkid")int gkid){
        List<Goods> goods = goodsService.selectid4(gkid);
        return goods;
    }
    //查看商品详细信息
    @RequestMapping(value = "/selectGoodsInfo/{gid}",method =RequestMethod.GET )
    @ResponseBody
    public GoodsInfo selectGoodsInfo(@PathVariable("gid")int gid){
       GoodsInfo goodsInfo = goodsService.selectGoodsInfo(gid);
        System.out.println(goodsInfo);
       return goodsInfo;
    }

    /*后台代码，勿动！！！！！！*/
    @RequestMapping("/adminall")
    @ResponseBody
    //page当前页
    //limt 每页行数
    //PageHeler.startPage(当前页，每页行数);----------测试
    //修改sql：select * from student limit ?,?----(page-1)*limit,limit
    //总行数：select counr(*) from student
    public Msg1<Goods> selectAll(@RequestParam(defaultValue = "1")Integer page,
                                 @RequestParam(defaultValue = "5")Integer limit){
        List<Goods> goods = goodsService.selectAll();
        PageInfo<Goods> pageInfo=goodsService.getGoodsByPage(page,limit);
        // System.out.println(pageInfo);
        Msg1<Goods> msg=new Msg1<>();
        //count属性类型改为long值
        msg.setCount(pageInfo.getTotal());
        msg.setCode(0);
        msg.setData(pageInfo.getList());
        return msg;
    }

    @RequestMapping("/admindel/{id}")
    @ResponseBody
    public AjaxResponse delete(@PathVariable("id") int id){
        System.out.println(id+"............................");
        goodsService.delete(id);
        return new AjaxResponse(true,"删除成功！");
    }

    @RequestMapping(value = "/insert" )
    @ResponseBody
    public AjaxResponse insert (Goods goods){
        System.out.println(goods);

        //Uploadword uploadword=new Uploadword();
        // String update = uploadword.update(pic, session);
        // goods.setGpic(update);
        int i = goodsService.insert(goods);
        System.out.println(i);
        // return "redirect:/admin/admin/goods";
        return new AjaxResponse(true,"加入成功！");
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public AjaxResponse update(Goods goods ) {
        int update = goodsService.update(goods);
        System.out.println("11111");
        if (update == 1) {
            return new AjaxResponse(true, "删除成功！");
        }else {
            return new AjaxResponse(false, "删除失败！");
        }
    }

    @RequestMapping(value = "/deleteSelect",method = RequestMethod.GET)
    @ResponseBody
    public AjaxResponse deleteSelect(@RequestParam(name = "idArry", required = true) String idArry){
        String[] idlist = idArry.split(",");
        List<String> list=new ArrayList<>();
        for (int i=0;i<idlist.length;i++){
            list.add(idlist[i]);
        }
        int deleteSelect = goodsService.deleteSelect(list);
        System.out.println(deleteSelect);
        return new AjaxResponse(true,"删除成功！");
    }

}
